package com.ruoyi.client.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.pojo.form.BookShelfAddForm;
import com.ruoyi.client.pojo.vo.BookShelfVo;
import com.ruoyi.client.service.BookShelfService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous  //接口允许匿名访问
@RestController
@RequestMapping("/bookshelf")
public class BookShelfController extends BaseController {
    private static final Logger logger= LoggerFactory.getLogger(BookShelfController.class);
    @Resource
    private BookShelfService bookShelfService;


    //获取当前用户购物车列表的所有记录
    @GetMapping("/list")
    public AjaxResult list(){
        Long userId= StpUtil.getLoginIdAsLong();
        List<BookShelfVo> list=bookShelfService.list(userId);
        return AjaxResult.success(list);
    }

    //添加书籍
    @PostMapping("/addBookShelf")
    public AjaxResult addBookShelf(@RequestBody @Valid BookShelfAddForm form){
        //先登录用户，未登录会抛出例外NotLoginException
        Long userId=StpUtil.getLoginIdAsLong();

        //书架
        BookShelfVo bookShelfVo =new BookShelfVo();
        BeanUtils.copyProperties(form,bookShelfVo);
        bookShelfVo.setUserId(userId);
        bookShelfVo.setCheck(true);

        if (bookShelfService.addBookShelf(bookShelfVo)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/deleteBookshelf")
    public AjaxResult deleteBookshelf(@RequestParam String ids){
        //用户登录验证
        StpUtil.checkLogin();
        if (bookShelfService.deleteBookshelf(ids)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/updateCheck")
    public AjaxResult updateCheck(Long id, boolean isCheck){
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();
        BookShelfVo bookShelfVo=new BookShelfVo();
        bookShelfVo.setId(id);
        bookShelfVo.setCheck(isCheck);
        if(bookShelfService.update(bookShelfVo)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @GetMapping("/updateCheckAll")
    public AjaxResult updateCheckAll(Boolean isCheck){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        if(bookShelfService.updateCheckAll(userId,isCheck)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @GetMapping("/listCheck")
    public AjaxResult listCheck(){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();
        return AjaxResult.success(bookShelfService.listCheck(userId));
    }

    @GetMapping("/deleteAll")
    public AjaxResult deleteAll(){
        // 当前登录用户,未登录会抛出例外NotLoginException
        Long userId = StpUtil.getLoginIdAsLong();

        if(bookShelfService.deleteAll(userId)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

}

